package com.ruoyi.web.core.config;

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * elasticsearch的日期转换器配置
 *
 * @author weixingyu
 */
@ReadingConverter
public class UtcToGmt8Converter implements Converter<String, LocalDateTime> {

    @Override
    public LocalDateTime convert(String source) {
        Instant instant = Instant.parse(source);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);

        // 转换为GMT+8时区的时间
//        ZoneId zoneId = ZoneId.of("Asia/Shanghai"); // 或者使用 "Asia/Shanghai"
//        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
//
//        // 格式化输出时间
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        String formattedDateTime = zonedDateTime.format(formatter);
        return localDateTime.plusHours(8);
    }
}